﻿using System;
using System.Collections.Generic;
using System.Linq;

namespace Diploma.Models
{
    public partial class Lecturer : ISoftDeletable
    {
        public new string FullName
        {
            get { return ToString(); }
        }

        public IEnumerable<LecturersLoading> StudentsLoading
        {
            get { return DataManager.Instance.LecturersLoadingsRepository.GetLoadingsForLecturer(this); }
        }

        #region ISoftDeletable Members

        public void MarkForDeletion()
        {
            Mark(true);
        }

        public void UnmarkFromDeletion()
        {
            Mark(false);
        }

        #endregion

        public new string ToString()
        {
            return string.Format("{0} {1} {2} {3}", Rank, Surname, Name, Patronymic);
        }

        public bool CanEditTask(Task task)
        {
            if (task.DeletionDate == null && task.CollaborationRoom.DeletionDate == null &&
                task.CollaborationRoom.Lecturer.DeletionDate == null && (task.CollaborationRoom.Lecturer == this || (task.CollaborationRoom.Consultant!= null && task.CollaborationRoom.Consultant == this) ))
                return true;
            return false;
        }

        private void Mark(bool asDeleted)
        {
            DeletionDate = asDeleted ? (DateTime?) DateTime.Now : null;
            CollaborationRooms.ToList().ForEach(delegate(CollaborationRoom room)
                                                    {
                                                        if (asDeleted) room.MarkForDeletion();
                                                        else room.UnmarkFromDeletion();
                                                    });

            LecturersLoadings.ToList().ForEach(delegate(LecturersLoading ll)
                                                   {
                                                       if (asDeleted) ll.MarkForDeletion();
                                                       else ll.UnmarkFromDeletion();
                                                   });
            Messages.ToList().ForEach(delegate(Message mes)
                                          {
                                              if (asDeleted) mes.MarkForDeletion();
                                              else mes.UnmarkFromDeletion();
                                          });
            PublicMessages.ToList().ForEach(delegate(PublicMessage pm)
                                                {
                                                    if (asDeleted) pm.MarkForDeletion();
                                                    else pm.UnmarkFromDeletion();
                                                });
        }
    }
}